<template>
	<view class="page">
		<view class="nav_bar" :style="`background: url(${$util.prefix('auth/store_auth_nav_bg.png')}) 0 0 no-repeat;`">
			<uni-nav-bar color="#333" left-icon="left" title="推广" :border='false' :fixed="true"
				background-color="transparent" @clickLeft="$native.backAction()"></uni-nav-bar>
		</view>

		<view class="header flex_r_between">
			<view class="money flex_c">
				<view>{{ balance || 0 }}</view>
				<text>我的余额(元)</text>
			</view>
			<view class="header_btn flex_r_around" @click="toRecharge">
				<text>充值</text>
			</view>
		</view>

		<view class="tab flex_r_between">
			<view class="tab_item flex_r_around" @click="tab = 1" :id="tab == 1 ? 'select' : ''">
				<text>推广任务</text>
			</view>
			<view class="tab_item flex_r_around" @click="tab = 2" :id="tab == 2 ? 'select' : ''">
				<text>说明</text>
			</view>
		</view>

		<view class="list" v-if="tab == 1">
			<view class="item" v-for="(item,index) in list" :key="index">
				<view class="top flex_center">
					<view class="left flex_center">
						<view class="avatar">
							<image :src="item.avatar" mode="aspectFill"></image>
						</view>
						<view class="text">
							<view class="nickname hidden">{{ item.name || '' }}</view>
							<view class="auth flex_center">

								<!-- 0=未完成，1=实名认证，2=技能认证，3=企业认证 -->

								<image :src="$util.prefix('circle/qi_icon.png')" mode="aspectFill" class="icon"
									v-if="item.owner_auth_status == 3"></image>

								<image :src="$util.prefix('circle/v_icon.png')" mode="aspectFill" class="icon"
									v-else-if="item.owner_auth_status > 0">
								</image>

								<text class="hidden">{{ item.owner_auth_name || '' }}</text>
							</view>
						</view>
					</view>

					<!-- 第一版本 增加推广状态：rec_show   0=待竞价，1=推广中，2=已完成，3=已停止 -->
					<!-- 第二版本 rec_show  0=待竞价，1=推广中，2=已停止，3=已欠费-->
					<view class="right">
						{{
							item.rec_show == 0 ? '待竞价'
							: item.rec_show == 1 ? '推广中'
							: item.rec_show == 2 ? '已停止'
							: item.rec_show == 3 ? '已欠费'
							: ''
						}}
					</view>

				</view>

				<view class="bottom flex_r_between">
					<view class="box">
						<text>出价(元/千次曝光)</text>
						<view>{{ item.unit_price || 0 }}</view>
					</view>
					<view class="box">
						<text>点击数(次)</text>
						<view>{{ item.click_num || 0 }}</view>
					</view>
					<view class="box">
						<text>入圈人数(个)</text>
						<view>{{ item.add_member_num || 0 }}</view>
					</view>
				</view>

				<view class="list_btn flex_r_around" @click="onRecShow(item)">
					<text v-if="item.rec_show == 0 || item.rec_show == 1">停止推广</text>
					<text v-if="item.rec_show == 2">开始推广</text>
				</view>


			</view>
		</view>

		<view class="rich" v-if="tab == 2">
			<view class="title">免费推荐说明</view>
			<view class="text">
				<view>1.为了用户体验，圈子内容必须完善，圈子用户数量大于1000人，才能到免费推荐列表。</view>
				<view>2.圈子用户活跃度，新增数量及交易金额（订单，会员，入圈费）越高被推荐的频次越高。</view>
			</view>
			<view class="title">竞价广告说明</view>
			<view class="text">
				<view>1.圈子推荐列表中，免费推荐和竞价广告资源各占50%。</view>
				<view>2.竞价广告出价越高获取的广告资源越多。</view>
				<view>3.竞价广告价格每10分钟更新一次，10分钟内广告余额不足将暂停推广。</view>
				<view>4.竞价广告分为推荐展示和搜索广告</view>
			</view>
		</view>

		<view>
			<liu-drag-button @clickBtn="show = true" :bottomPx="260">
				<u-icon name="plus" color="#FFF" size="34rpx" bold></u-icon>
			</liu-drag-button>
		</view>

		<view>
			<u-popup :show="show" @close="show = false" bgColor="transparent" mode="center">
				<view class="modal">
					<view class="icon flex_r_between">
						<text></text>
						<u-icon name="close-circle" color="#CCCCCC" size="48rpx" @click="show = false"></u-icon>
					</view>

					<view class="modal_title">推广任务</view>

					<view class="modal_circle flex_center">
						<view class="avatar">
							<image :src="circle.avatar" mode="aspectFill"></image>
						</view>
						<view class="text">
							<view class="nickname hidden">{{ circle.name }}</view>
							<view class="auth flex_center">
								<!-- 认证类型:0=没有，1=实名认证（优先展示），2=身份认证，3=公司认证 -->
								<image :src="$util.prefix('circle/qi_icon.png')" mode="aspectFill" class="icon"
									v-if="circle.owner_auth_status == 3"></image>

								<image :src="$util.prefix('circle/v_icon.png')" mode="aspectFill" class="icon"
									v-else-if="circle.owner_auth_status > 0"></image>

								<text class="hidden">{{ circle.owner_auth_name || '' }}</text>
							</view>
						</view>
					</view>

					<view class="modal_input">
						<view class="top flex_r_between">
							<view class="label">出价</view>
							<view class="inp flex_center">
								<input type="number" placeholder-class="input-placeholder" placeholder="请输入"
									maxlength="6" v-model="unit_price" />
								<text>元/千次曝光</text>
							</view>
						</view>
						<view class="bottom">当前平均价格{{ now_avg_price }}元/千次曝光</view>
					</view>

					<view class="modal_text">推广位置：推荐首页</view>

					<view class="modal_btn flex_r_around" @click="submitPopularize">
						<text>开始推广</text>
					</view>

				</view>
			</u-popup>
		</view>

		<u-popup :show="show_chat" @close="show_chat = false" bgColor="transparent" mode="center">
			<view class="modal">
				<view class="icon flex_r_between">
					<text></text>
					<u-icon name="close-circle" color="#CCCCCC" size="48rpx" @click="show_chat = false"></u-icon>
				</view>

				<view class="modal_title">请联系客服充值</view>

				<view class="modal_btn flex_r_around" @click="$native.toChat({
						nickName: '超圈官方',
						avatar: 'https://qiniu-cdn.maeiyun.com//h5static/logo.png',
						user_id: '1',
					})">
					<text>联系客服</text>
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				tab: 1,

				show: false,

				show_chat: false,

				balance: '',

				circle_id: 0,
				circle: {},

				list: [],
				now_avg_price: 0,

				unit_price: '',
			}
		},
		methods: {

			toRecharge() {
				if (uni.getSystemInfoSync().platform == 'android') {
					this.$util.toUrl('/pages/circle/recharge')
				} else {
					this.show_chat = true;
				}
			},

			getBalance() {
				this.$get('/popularize/getBalance').then(res => {
					this.balance = res.data.balance;
				})
			},


			getInitInfo() {
				this.$get('/circle.circlePopularize/getInitInfo', {
					circle_id: this.circle_id
				}).then(res => {
					this.list = res.data.popuList;
					this.now_avg_price = res.data.now_avg_price;
				})
			},

			getCircleInfo() {
				this.$get('/circle.circle/getCircleInfo', {
					circle_id: this.circle_id
				}).then(res => {
					this.circle = res.data;
				});
			},


			submitPopularize() {
				this.$post('/circle.circlePopularize/submitPopularize', {
					unit_price: this.unit_price,
					circle_id: this.circle_id,
					act: 1,
				}).then(res => {
					this.show = false;
					this.$util.msg(res.msg);
					this.getInitInfo();
				})
			},

			onRecShow(item) {
				var rec_show = item.rec_show;
				// act: 操作类型：1=开始推广，2=停止推广
				this.$post('/circle.circlePopularize/submitPopularize', {
					act: item.rec_show == 0 || item.rec_show == 1 ? 2 : 1,
					circle_id: item.id,
					unit_price: item.unit_price,
				}).then(res => {
					this.$util.msg('已开始');
					this.getInitInfo();
				})
			},


		},

		onLoad(option) {
			if (option.token) {
				uni.setStorageSync('token', option.token);
				uni.setStorageSync('cid', option.cid);
				uni.setStorageSync('terminal', option.terminal);
			}
			this.circle_id = option.circle_id || 0;
			this.getBalance();
			this.getInitInfo();
			this.getCircleInfo();
		},

		onShow() {
			this.getBalance();
		},

	}
</script>

<style lang="less" scoped>
	.page {
		min-height: 100vh;
		background: #F1F1F1;
		padding: calc(44px + 32rpx + var(--paging-head-top)) 24rpx 30rpx;
	}

	.nav_bar {
		background-size: 100% !important;
	}

	.header {
		height: 240rpx;
		background: #FFFFFF;
		border-radius: 24rpx;
		backdrop-filter: blur(4px);
		padding: 0 40rpx;

		.money {
			align-items: flex-start;

			view {
				font-weight: 600;
				font-size: 64rpx;
				color: #131313;
			}

			text {
				font-size: 24rpx;
				color: #999999;
			}
		}

		.header_btn {
			width: 144rpx;
			height: 56rpx;
			background: #FD2A53;
			border-radius: 28rpx;

			text {
				font-size: 28rpx;
				color: #FFFFFF;
			}
		}
	}

	.tab {
		margin: 20rpx 0;

		.tab_item {
			width: 340rpx;
			height: 80rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			border: 2rpx solid #E5E5E5;

			text {
				font-weight: 500;
				font-size: 28rpx;
				color: #333333;
			}
		}

		#select {
			background: #FFEAEE;
			border: 2rpx solid #FFC6D1;

			text {
				color: #FD2A53;
			}
		}
	}

	.list {
		.item {
			background: #FFFFFF;
			border-radius: 24rpx;
			backdrop-filter: blur(4px);
			padding: 32rpx;
			overflow: hidden;
			margin-bottom: 20rpx;

			.top {
				overflow: hidden;

				.right {
					font-weight: 500;
					font-size: 32rpx;
					color: #FD2A53;
					line-height: 44rpx;
				}

				.left {
					flex: 1;
					overflow: hidden;

					.avatar {
						width: 124rpx;
						height: 124rpx;
						border-radius: 62rpx;
						overflow: hidden;
						margin-right: 16rpx;

						image {
							width: 124rpx;
							height: 124rpx;
						}
					}

					.text {
						flex: 1;
						overflow: hidden;

						.nickname {
							font-weight: 500;
							font-size: 32rpx;
							color: #131313;
							margin-bottom: 10rpx;
						}

						.auth {
							overflow: hidden;

							image {
								width: 28rpx;
								height: 28rpx;
								margin-right: 8rpx;
							}

							text {
								flex: 1;
								font-size: 24rpx;
								color: #999999;
							}
						}
					}
				}
			}

			.bottom {
				margin-top: 24rpx;

				.box {
					text {
						font-weight: 400;
						font-size: 26rpx;
						color: #999999;
					}

					view {
						font-weight: 500;
						font-size: 32rpx;
						color: #333333;
					}
				}
			}

			.list_btn {
				width: 360rpx;
				height: 88rpx;
				background: #FD2A53;
				border-radius: 44rpx;
				margin: 32rpx auto 0;

				text {
					font-size: 28rpx;
					color: #FFFFFF;
					line-height: 40rpx;
				}
			}

		}
	}

	.rich {
		background: #FFFFFF;
		border-radius: 24rpx;
		backdrop-filter: blur(4px);
		padding: 32rpx;

		.title {
			font-weight: 500;
			font-size: 32rpx;
			color: #131313;
			line-height: 44rpx;
		}

		.text {
			margin: 24rpx 0 48rpx;

			view {
				font-size: 26rpx;
				color: #666666;
				line-height: 36rpx;
			}
		}
	}

	.modal {
		width: 616rpx;
		background: #FFFFFF;
		border-radius: 40rpx;
		padding: 16rpx 18rpx 64rpx;

		.modal_title {
			font-weight: 600;
			font-size: 36rpx;
			color: #131313;
			line-height: 50rpx;
			margin: -16rpx 0 32rpx;
			text-align: center;
		}

		.modal_circle {
			padding: 0 30rpx;

			.avatar {
				width: 124rpx;
				height: 124rpx;
				border-radius: 62rpx;
				overflow: hidden;
				margin-right: 16rpx;

				image {
					width: 124rpx;
					height: 124rpx;
				}
			}

			.text {
				flex: 1;
				overflow: hidden;

				.nickname {
					font-weight: 500;
					font-size: 32rpx;
					color: #131313;
					margin-bottom: 10rpx;
				}

				.auth {
					overflow: hidden;

					image {
						width: 28rpx;
						height: 28rpx;
						margin-right: 8rpx;
					}

					text {
						flex: 1;
						font-size: 24rpx;
						color: #999999;
					}
				}
			}
		}

		.modal_input {
			background: #F1F1F1;
			border-radius: 16rpx;
			margin: 32rpx 30rpx 24rpx;

			.top {
				padding: 20rpx 32rpx;
				border-bottom: 2rpx solid #E3E3E3;

				.label {
					font-size: 28rpx;
					color: #131313;
				}

				.inp {
					input {
						width: 144rpx;
						height: 56rpx;
						background: #FFFFFF;
						border-radius: 12rpx;
						text-align: center;
						font-size: 28rpx;
						color: #333;
					}

					text {
						font-size: 28rpx;
						color: #131313;
						margin-left: 12rpx;
					}
				}
			}

			.bottom {
				font-size: 24rpx;
				color: #666666;
				text-align: right;
				padding: 14rpx 32rpx;
			}
		}

		.modal_text {
			margin: 0 30rpx 48rpx;
			font-size: 26rpx;
			color: #131313;
			margin-bottom: 48rpx;
		}

		.modal_btn {
			width: 360rpx;
			height: 88rpx;
			background: #FD2A53;
			border-radius: 44rpx;
			margin: 0 auto;

			text {
				font-size: 28rpx;
				color: #FFFFFF;
			}
		}

	}
</style>